const http = require('superagent');
const URL = 'https://www.pixiv.net/touch/ajax/illust/details';
const log4js = require('./log4js');
const async = require('async');
const logger = log4js.getLogger('logs');
const sequelize = require('./sequelize');
const Sequelize = require('sequelize');
const Pixiv = sequelize.define('pixiv',{
    pixivId:{ type: Sequelize.BIGINT(11), autoIncrement: true,primaryKey:true},
    title:Sequelize.STRING,
    fileName:Sequelize.STRING,
    userName:Sequelize.STRING,
    imgCount:Sequelize.INTEGER,
    tag:Sequelize.STRING,
    uploadTime:Sequelize.BIGINT(11),
    oldId:Sequelize.BIGINT(11),
    userId:Sequelize.BIGINT(11),
    collectNum:Sequelize.BIGINT(11),
    step:{type:Sequelize.BOOLEAN,defaultValue:1},
    downloadUrl:Sequelize.STRING
},{
    timestamps:false,
    freezeTableName:true,
});
let i = 31015941;
async function get(illust_id,cb) {
    http.get(URL)
        .query({mode:'medium'})
        .query({illust_id})
        .end((err,res) =>{
            if(err) {
                logger.error(`ERROR:${err}`);
                cb();
            }
            if(res.body && res.body.isSucceed){
                let {illust_details,author_details} = res.body;
                let {tags,upload_timestamp,id,title,bookmark_user_total,page_count,url_big} = illust_details;
                if(parseInt(bookmark_user_total) < 100) {
                    cb();
                    return;
                }
                //预存储
                let fileName = url_big;
                //处理标签
                tags = tags.join("|");
                //图片数
                page_count = parseInt(page_count);
                //作者信息
                let userId = author_details.user_id || 0;
                let userName = author_details.user_name || 'unknow';
                let downloadUrl = [];
                if(page_count > 1){
                    let urls = illust_details.manga_a;
                    for (let i in urls){
                        downloadUrl.push(urls[i]['url_big']);
                    }
                }
                downloadUrl = JSON.stringify(downloadUrl);
                //保存
                let arg = {
                    tag:tags,
                    fileName,
                    uploadTime:upload_timestamp,
                    oldId:id,
                    title,
                    collectNum:bookmark_user_total,
                    imgCount:page_count,
                    userId,
                    userName,
                    downloadUrl
                };
                Pixiv.create(arg).then(() =>{
                    logger.info(`创建拉取成功，第 ${illust_id} 条数据`);
                    cb();
                }).catch(err =>{
                    logger.error(`insert err:${err}`);
                    cb();
                });
            }else {
                logger.info(`第${illust_id}条暂无数据`);
                cb();
            }
        });
}

function getData() {
    let arg = new Array(100);
    async.mapLimit(arg,20,(err,res) =>{
        get(i,res);
        i++;
    },(err,res) =>{
        console.log('finish');
        getData();
    });
}
getData();